<?php
namespace app\controller;

use think\Request;
use app\model\NavSiteType;
use app\model\ProjectComment;
use app\model\BbsComment;
use app\common\UtilService;
use app\validate\UserValidate;
use think\helper\Str;
use app\model\ProjectCollection;
use app\model\ProjectLike;
use app\validate\ProjectCollectionValidate;
use app\model\Bbs;
use app\validate\BbsValidate;
use app\model\BbsLike;
use app\model\UserFans;
use app\model\Book;
use app\model\BbsTypeFans;
use app\validate\BbsTypeValidate;
use app\model\BbsType;
use think\facade\Db;


/**
 * 需要登录才能操作的异步提交方法
 */

class AjaxSyn extends Base
{
	/**
	 * [circleEdit 新建圈子]
	 * @since  2022-05-06
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function circleEdit(Request $request)
	{
		try {

			$param = UtilService::postMore([
	            ['id', ''],
	            ['type_name', ''],
	            ['type_desc', ''],
	            ['type_pic', ''],
	            ['type_tags', ''],
	            ['type_status', ''],
	            ['type_class', ''],
	            ['update_time', time()],
	        ]);

			validate(BbsTypeValidate::class)->scene('edit')->check($param);

			$res = BbsType::whereNull('delete_time')->find($param['id']);
			if(!$res){
				return error('圈子不存在');
			}

			$res->type_name = $param['type_name'];
			$res->type_desc = $param['type_desc'];
			$res->type_pic = $param['type_pic'];
			$res->type_tags = $param['type_tags'];
			$res->type_status = $param['type_status'];
			$res->type_class = $param['type_class'];
			$res->update_time = time();
			$res->save();

			return success($res, '编辑圈子成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [circleAdd 新建圈子]
	 * @since  2022-05-06
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function circleAdd(Request $request)
	{
		try {

			$param = UtilService::postMore([
	            ['type_name', ''],
	            ['type_desc', ''],
	            ['type_pic', ''],
	            ['type_tags', ''],
	            ['type_status', ''],
	            ['type_class', ''],
	            ['create_time', time()],
	            ['update_time', time()],
	            ['update_time', time()],
	            ['type_ids', uniqid()],
	            ['user_id', $request->userId],
	        ]);

			validate(BbsTypeValidate::class)->scene('add')->check($param);

			Db::startTrans();
			$res = BbsType::create($param);

			if($res){

				BbsTypeFans::create([
					'bbs_type_id' => $res['id'],
					'is_admin'    => 1,
					'user_id' 	  => $request->userId,
					'create_time' => time(),
					'update_time' => time(),
				]);

				Db::commit();
				return success($res, '创建圈子成功');
			}

			return error('创建圈子失败');
	        
        } catch (\Exception $e) {

        	Db::rollback();
            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [joinCircle 加入或者退出圈子]
	 * @since  2022-05-05
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function joinCircle(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['bbs_type_id', ''],
	            ['user_id', $request->userId],
	        ]);

			$res = BbsTypeFans::where($param)->find();
			if($res){

				if($res['is_admin'] == 1){
					return error('圈主管理员不能退出');
				}

				$res = BbsTypeFans::where($param)->delete();

				return success($res, '退出圈子成功');
			}

			$param['is_admin'] 	  = 0;
			$param['create_time'] = time();
			$param['update_time'] = time();

			$res2 = BbsTypeFans::create($param);

			if($res2){
				return success($res2, '加入圈子成功');
			}

			return error('加入圈子失败');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [index 评论或者回复bbs动态]
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function bbsMsg(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['bbs_id',''],
	            ['user_id', $request->userId],
	            ['parent_id',0],
	            ['msg',''],
	            ['create_time', time()],
	            ['update_time', time()],
	        ]);

			$res = BbsComment::create($param);

			if($res){
				return success($res, '评论成功');
			}

			return error('评论失败');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}


	/**
	 * [downBook 下载电子书资源]
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function downBook(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['book_id', $request->userId],
	        ]);

			$res = Book::whereNull('delete_time')->where('id', $param['book_id'])->value('downloads_links');

			if($res){
				Book::whereNull('delete_time')->where('id', $param['book_id'])->inc('downloads_num')->update();
			}
			
			return success($res, '操作成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [follow 关注与取消关注]
	 * @since  2022-04-08
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function follow(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['user_id', $request->userId],
	            ['fans_id',''],
	        ]);

			if($param['fans_id'] == ''){
				return error('关注有误!');
			}

			if($param['user_id'] == $param['fans_id']){
				return error('不能关注自己!');
			}

			$res = UserFans::where($param)->find();
			if($res){
				$res->delete();
				$type = 0;
			} else{
				$param['create_time'] = time();
				UserFans::create($param);
				$type = 1;
			}
			
			return success($type, '操作成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [bbsRelease 点赞]
	 * @since  2022-04-06
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function bbsLike(Request $request)
	{
		try {

			$param = UtilService::postMore([
	            ['user_id', $request->userId],
	            ['bbs_id',''],
	        ]);

			validate(BbsValidate::class)->scene('like')->check($param);

			$ck = BbsLike::where($param)->find();
			$arr = [];
			if($ck){
				$res = $ck->delete();
				$arr['type'] = 0;
				$arr['msg']  = '取消点赞';
			} else{
				$param['create_time'] = time();
				$res = BbsLike::create($param);
				$arr['type'] = 1;
				$arr['msg']  = '已点赞';
			}

			$detail = Bbs::getBbsInfo([
				'id'      => $param['bbs_id'],
				'user_id' => $param['user_id'],
			]);
			$arr['detail'] = $detail;

			if($res){
				return success($arr, '已点赞');
			}

			return error('点赞失败');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [bbsRelease 发布动态]
	 * @since  2022-04-06
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function bbsRelease(Request $request)
	{
		try {

			$param = UtilService::postMore([
	            ['user_id', $request->userId],
	            ['bbs_imgs',''],
	            ['type_id', 0],
	            ['content',''],
	            ['create_time', time()],
	            ['update_time', time()],
	        ]);

			validate(BbsValidate::class)->scene('release')->check($param);

			$res = Bbs::create($param);

			if($res){
				return success($res, '发布成功');
			}

			return error('发布失败');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [index 开源项目评论]
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function projectComment(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['project_id',''],
	            ['user_id', $request->userId],
	            ['parent_id',0],
	            ['msg',''],
	            ['create_time', time()],
	            ['update_time', time()],
	        ]);

			$res = ProjectComment::create($param);

			if($res){
				return success($res, '评论成功');
			}

			return error('评论失败');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [projectComment 开源项目收藏]
	 * @since  2022-03-28
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function projectCollection(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['project_id',''],
	            ['user_id', $request->userId],
	        ]);

		    validate(ProjectCollectionValidate::class)->scene('collection')->check($param);
		    
		    $check = ProjectCollection::whereNull('delete_time')->where($param)->find();

		    $arr = array();
		    if($check){

		    	$check->delete();
		    	$arr['type'] = 'dec';
		    	$arr['text'] = '已取消收藏';

		    } else{

		    	ProjectCollection::create([
		    		'project_id'  => $param['project_id'],
		    		'user_id'     => $param['user_id'],
		    		'create_time' => time(),
		    		'update_time' => time(),
		    	]);

		    	$arr['type'] = 'inc';
		    	$arr['text'] = '收藏成功';
		    }

			
			return success($arr, '操作成功');

	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [projectComment 开源项目点赞喜欢]
	 * @since  2022-03-28
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function projectLike(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['project_id',''],
	            ['user_id', $request->userId],
	        ]);

		    validate(ProjectCollectionValidate::class)->scene('like')->check($param);
		    
		    $check = ProjectLike::whereNull('delete_time')->where($param)->find();

		    $arr = array();
		    if($check){

		    	$check->delete();
		    	$arr['type'] = 'dec';
		    	$arr['text'] = '已取消喜欢';

		    } else{

		    	ProjectLike::create([
		    		'project_id'  => $param['project_id'],
		    		'user_id'     => $param['user_id'],
		    		'create_time' => time(),
		    		'update_time' => time(),
		    	]);

		    	$arr['type'] = 'inc';
		    	$arr['text'] = '已喜欢';
		    }

			
			return success($arr, '操作成功');

	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}
}